﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Easy.BLL;
using Easy.Model;
using Easy.Operation;

namespace Easy.Web.Pages.EntityPages
{
    public partial class EntityAppFormConfigPage : BasePage
    {
        public string Fields = "";
        public string entityname = "";
        public string entityid = "";
        public string formname = "";

        FormBaseBLL fbb;
        private JsHubBLL JsHub = new JsHubBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            entityname = Request.Params["entityname"];
            entityid = Request.Params["Id"];
            formname = Request.Params["formname"] == null ? "" : Request.Params["formname"];
            fbb = new FormBaseBLL(entityname);

            Core c = new Core(Request.Params["entityname"]);

            foreach (KeyValuePair<string, Field> kvp in c.GetFields())
            {
                Fields += string.Format("<li {3} role='presentation' okey='{2}' atttype={1} class='active'><a href='#'>{0}</a></li>",
                     kvp.Value.DisplayName,
                     kvp.Value.Type,
                     kvp.Key,
                    string.Format("_innerHTML=\"{0}\"", Server.HtmlEncode(getHTML(kvp.Key, kvp.Value.DisplayName, kvp.Value.Type.ToString(), kvp.Value.LookupEntityName, kvp.Value.IsNullable)))
                 );
            }
            if (!IsPostBack)
            {
                initForm();
            }
        }
        private void initForm()
        {

            if (formname != "")
            {
                List<string> formStruncture = fbb.getFormStructure(formname);
                List<JsList> jslist = JsHub.GetAllJsList(entityname, formname, "form");
                JsList.Items.Clear();
                foreach (JsList jsList in jslist)
                {
                    var item = new ListItem(jsList.JsName, jsList.JsListId.ToString());
                    item.Selected = jsList.JsCount > 0 ? true : false;
                    JsList.Items.Add(item);
                }
                if (formStruncture != null)
                {
                    content.Text = formStruncture[0];
                    span_formname.InnerText = formStruncture[1];
                    //jscontent_Src.Text = formStruncture[3];
                    jscontent_beforeLoad.Text = formStruncture[4];
                    jscontent_afterLoad.Text = formStruncture[5];
                    jscontent_beforeSave.Text = formStruncture[6];
                    jscontent_afterSave.Text = formStruncture[7];
                }
            }
            else
            {
                span_formname.InnerText = "[创建]";
            }
        }

        private string getHTML(string id, string name, string type, string entity, bool? isnullable = false)
        {
            string reHTML = "";
            bool isnull = false;
            switch (type)
            {
                case "nvarchar":
                    reHTML = EntityAppFormConfigPage.Templete.Str(id, name, type, isnull);
                    break;
                case "int":
                    reHTML = EntityAppFormConfigPage.Templete.Int(id, name, type, isnull);
                    break;
                case "float":
                    reHTML = EntityAppFormConfigPage.Templete.Float(id, name, type, isnull);
                    break;
                case "date":
                    reHTML = EntityAppFormConfigPage.Templete.FormDate(id, name, type, isnull);
                    break;
                case "datetime":
                    reHTML = EntityAppFormConfigPage.Templete.FormTime(id, name, type, isnull);
                    break;
                case "ntext":
                    reHTML = EntityAppFormConfigPage.Templete.Ntext(id, name, type, isnull);
                    break;
                case "key":
                    reHTML = EntityAppFormConfigPage.Templete.Str(id, name, type, isnull);
                    break;
                case "picklist":
                    reHTML = EntityAppFormConfigPage.Templete.Picklist(id, name, type, isnull);
                    break;
                case "bit":
                    reHTML = EntityAppFormConfigPage.Templete.Radio(id, name, type, isnull);
                    break;
                case "lookup":
                    reHTML = EntityAppFormConfigPage.Templete.Lookup(id, name, type, entity, isnull);
                    break;
                case "password":
                    reHTML = EntityAppFormConfigPage.Templete.Password(id, name, type, isnull);
                    break;
            }
            return reHTML;
        }
        public static class Templete
        {
            public static string Lookup(string id, string name, string type, string entity, bool isnecessary)
            {
                string templete =
                "\n<li class=\"mui-table-view-cell\" id='c_{0}' tl-for='{0}' tl-type=\"{2}\" tl-lookupentity=\"{3}\">\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\" tl-lookupentity=\"{3}\">\n" +
                     "<label class=\"mui-text-center\" title='{1}' tl-for='{0}' tl-type=\"{2}\" tl-lookupentity=\"{3}\">{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                                  "<input name=\"{0}\" class=\"mui-input\" readonly=\"true\" id='{0}'  type=\"text\" tl-for='{0}' tl-type=\"{2}\" tl-lookupentity=\"{3}\" placeholder=\"请选择{1}\">\n" +
                           "</div>\n" +
                           "<div type=\"button\" class=\"mui-btn mui-btn-primary\" tl-for='{0}' tl-type=\"{2}\" tl-lookupentity=\"{3}\">选择</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower(), entity.ToLower());
            }
            public static string Str(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}'  tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                    "<input type='text' id='{0}' class='mui-input-clear mui-input' name='{0}' tl-for='{0}' tl-type=\"{2}\" placeholder=\"请输入{1}\"  />\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string Picklist(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class=\"mui-table-view-cell\" id='c_{0}'tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                    "<input name=\"{0}\" id='{0}' class=\"mui-input-clear mui-input\" type=\"text\" tl-for='{0}' tl-type=\"{2}\" placeholder=\"请选择{1}\"/>\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string FormDate(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                        "<input type='text' id='{0}' class='mui-input-clear mui-input' name='{0}' tl-for='{0}' tl-type=\"{2}\" placeholder=\"请选择{1}\" />\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());


            }
            public static string FormTime(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                        "<input type='text' id='{0}' class='mui-input-clear mui-input' name='{0}' tl-for='{0}' tl-type=\"{2}\" placeholder=\"请选择{1}\" />\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string Int(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                        "<input type='number' id='{0}' class='mui-input-clear mui-input' id='{0}' tl-for='{0}' tl-type=\"{2}\" name='{0}' placeholder=\"请输入{1}\" />\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string Float(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                        "<input type='number' id='{0}' class='mui-input-clear mui-input' id='{0}' tl-for='{0}' tl-type=\"{2}\" name='{0}' placeholder=\"请输入{1}\" />\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string Ntext(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<textarea class=\"mui-input-group\" id='{0}' tl-for='{0}' tl-type=\"{2}\" name='{0}' placeholder=\"请输入{1}\" rows=6 style=\"padding: 5%;width:100%;\"></textarea>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string Radio(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                "<div class=\"mui-switch mui-active\" tl-for='{0}' tl-type=\"{2}\">\n" +
                "<div class=\"mui-switch-handle\" id='{0}'  tl-for='{0}' tl-type=\"{2}\"></div>\n" +
                "</div>\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
            public static string Password(string id, string name, string type, bool isnecessary)
            {
                string templete =
                "\n<li class='mui-table-view-cell' id='c_{0}' tl-for='{0}' tl-type=\"{2}\" >\n" +
                "<!--**********begin_" + id + "**********-->\n" +
                "<div class='mui-input-row' tl-for='{0}' tl-type=\"{2}\">\n" +
                    "<label tl-for='{0}' tl-type=\"{2}\" class=\"mui-text-center\" title='{1}' >{1}" + (isnecessary ? "<font class=\"tl-notnull\">*</font>" : "") + "</label>\n" +
                    "<input type='password' id='{0}' class='mui-input-clear mui-input' name='{0}' tl-for='{0}' tl-type=\"{2}\" placeholder=\"请输入{1}\"  />\n" +
                "</div>\n" +
                "<!--**********end_" + id + "**********-->\n" +
                "</li>";
                return string.Format(templete, id.ToLower(), name.ToLower(), type.ToLower());
            }
        }

        protected void SaveFormStructure_Click(object sender, EventArgs e)
        {
            string formHtml = HttpUtility.HtmlDecode(content.Text);
            //string str = jscontent_Src.Text;
            fbb.SaveFormStructure(formname, formHtml, "", "", jscontent_beforeLoad.Text, jscontent_afterLoad.Text, jscontent_beforeSave.Text, jscontent_afterSave.Text);
            var selected = JsList.GetSelectedIndices();
            List<Guid> lis = selected.Select(t => Guid.Parse(JsList.Items[t].Value)).ToList();
            JsHub.ModifyFormJsRelation(entityname, formname, lis);
            content.Text = formHtml;
        }

        protected void SaveFormStructure_Create_Click(object sender, EventArgs e)
        {
            string formHtml = HttpUtility.HtmlDecode(content.Text);
            fbb.CreateFormStructure(FormNameModalName.Value, FormNameModalDisplayName.Value, formHtml, "", jscontent_beforeLoad.Text, jscontent_afterLoad.Text, jscontent_beforeSave.Text, jscontent_afterSave.Text, GetSystemUser().SystemUserId.ToString(), 1);
            var selected = JsList.GetSelectedIndices();
            List<Guid> lis = selected.Select(t => Guid.Parse(JsList.Items[t].Value)).ToList();
            JsHub.ModifyFormJsRelation(entityname, formname, lis);
            content.Text = formHtml;
            Response.Redirect("/Pages/EntityPages/EntityAppFormConfigPage.aspx?Id=" + entityid + "&entityname=" + entityname + "&formname=" + FormNameModalName.Value, false);
        }
    }
}